#include "dialog.h"
#include "ui_dialog.h"
#include "QPainter"
#include "QPainterPath"
#include "QFile"
#include "QScreen"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);


    // 初始化标题栏;
    initTitleBar();
}


void Dialog::initTitleBar()
{
    m_titleBar = new MyTitleBar(this);
    m_titleBar->move(0, 0);
    m_titleBar->setButtonType(MIN_MAX_BUTTON);
    m_titleBar->setTitleIcon(QString(":/icons/images/LogosApacheSupersetIcon.svg"));

    connect(m_titleBar, &MyTitleBar::signalButtonMinClicked, this, &Dialog::onButtonMinClicked);
    connect(m_titleBar, &MyTitleBar::signalButtonRestoreClicked, this, &Dialog::onButtonRestoreClicked);
    connect(m_titleBar, &MyTitleBar::signalButtonMaxClicked, this, &Dialog::onButtonMaxClicked);
    connect(m_titleBar, &MyTitleBar::signalButtonCloseClicked, this, &Dialog::onButtonCloseClicked);


}

void Dialog::loadStyleSheet(const QString &sheetName)
{
    QFile file(":/Resources/" + sheetName + ".css");
    file.open(QFile::ReadOnly);
    if (file.isOpen())
    {
        QString styleSheet = this->styleSheet();
        styleSheet += QLatin1String(file.readAll());
        this->setStyleSheet(styleSheet);
    }
    else
        return;
}

void Dialog::onButtonMinClicked()
{
    showMinimized();
}

void Dialog::onButtonRestoreClicked()
{
    QPoint windowPos;
    QSize windowSize;
    m_titleBar->getRestoreInfo(windowPos, windowSize);
    this->setGeometry(QRect(windowPos, windowSize));
}

void Dialog::onButtonMaxClicked()
{
    m_titleBar->saveRestoreInfo(this->pos(), QSize(this->width(), this->height()));
    QRect desktopRect = QGuiApplication::primaryScreen()->availableGeometry();
    QRect FactRect = QRect(desktopRect.x() - 3, desktopRect.y() - 3, desktopRect.width() + 6, desktopRect.height() + 6);
    setGeometry(FactRect);
}

void Dialog::onButtonCloseClicked()
{
    close();
}

Dialog::~Dialog()
{
    delete ui;
}

